package com.jsx.model;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Conns{
    public Connection conn=null;
    public Statement stmt=null;
    public ResultSet rs=null;
    public  Conns(){
        try{
            Class.forName("com.mysql.jdbc.Driver");
        }catch(java.lang.ClassNotFoundException e){
            System.err.println(e.getMessage());
        }
    }

    public Connection  getCon(){
        try{
            conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/wangba_dbo?user=root&password=123456&useUnicode=true");

        }catch(SQLException ex){
            System.err.println(ex.getMessage());
        }finally{}
        return conn;
    }


    public ResultSet executeQuery(String sql){
        try{

            conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/wangba_dbo?user=root&password=123456&useUnicode=true");
            stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            rs=stmt.executeQuery(sql);
        }catch(SQLException ex){
            System.err.println(ex.getMessage());
        }finally{}
        return rs;
    }

    public ResultSet GetRs(String SQL){
        try
        {
            conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/wangba_dbo?user=root&password=123456&useUnicode=true");
            Statement Smt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
            ResultSet Rs = Smt.executeQuery(SQL);
            return Rs;
        }
        catch(SQLException e)
        {
            System.out.println("");
            return null;
        }
    }
    public int GetCount(String SQL){
        try
        {
            conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/wangba_dbo?user=root&password=123456&useUnicode=true");

            Statement Smt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

            int counts = Smt.executeUpdate(SQL);

            return counts;

        }
        catch(SQLException e)
        {
            System.out.println("");
            return 0;
        }
    }
    public boolean executeUpdate(String sql) {


        try {
            conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/wangba_dbo?user=root&password=123456&useUnicode=true");
            Statement stmt = conn.createStatement();
            int iCount = stmt.executeUpdate(sql);
            System.out.println("" + String.valueOf(iCount));
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            System.out.println("executeUpdaterError!");
        }
        return true;
    }

    public void close(){
        try {
            if (rs != null) rs.close();
        }
        catch (Exception e) {
            e.printStackTrace(System.err);
        }finally{}
        try {
            if (stmt != null) stmt.close();
        }
        catch (Exception e) {
            e.printStackTrace(System.err);
        }finally{}
        try {
            if (conn != null) {
                conn.close();
            }
        }
        catch (Exception e) {
            e.printStackTrace(System.err);
        }finally{}
    }
}
